import java.util.Timer;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @brief The runner class with the main method
 */
public final class SimulationRunner 
{
	private static Timer timer = new Timer();
	private static boolean isExploded = false;
	
	public static void main(String[] args) throws Exception 
	{	
		final int sleepTimeInMs = 50;
		
		final Map map = new Map();
		
		final Display display = new Display();
		display.setMap(map);
		
		final ShowInfo showInfo = new ShowInfo();
		showInfo.setWindowHeight(1*Entity.getEntityHeight());
		showInfo.setWindowWidth(map.getColumns()*Entity.getEntityWidth());
		
		final JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.add(showInfo);
		mainPanel.add(display);
		
		final CollisionDetector collisionDetector = new CollisionDetector(map);
		
		final JFrame frame = new JFrame("Bomberman");
		
		frame.addKeyListener(map.getUser());
		frame.setFocusable(true);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(mainPanel);
		frame.pack();
		frame.setVisible(true);
		
		while (true)
		{
			map.stepAll();
			
			showInfo.setLifeLeft(map.getLifeLeft());
			showInfo.setPointsGained((map.getPoints()));
			showInfo.setTime(map.getTimeLeft());
			showInfo.repaint();
			
			display.repaint();
			
			collisionDetector.detectUserMonsterCollision();
			
			if (isExploded) {
				collisionDetector.detectBombEntityCollision();
			}
			
			Thread.sleep(sleepTimeInMs);
		}
	}
	
	public static Timer getTimer() {
		return timer;
	}
	
	public static void refreshTimer() {
		timer = new Timer();
	}
	
	public static void setIsExploded(boolean arg) {
		isExploded = arg;
	}
}